<?php
class Model_Search extends Model{

    public function getSearchInfo($keyword,$sort,$offset,$pageSize){

        if(!empty($sort)){

            switch ($sort) {
                case 'popularity':

                    $orderby=" order by t.view desc,t.praiseup,t.wish ";

                    break;

                case 'new':

                    $orderby=" order by t.created desc ";

                    break;

                case 'review':

                    $orderby=" order by t.review desc ";

                    break;

                default:

                    $orderby=" order by t.created desc ";
                    break;

            }
        }

        else{

            $orderby=" order by t.created desc ";
        }

        $data=DB::select(array("postid",'id'),'postid','title','description','images','view','reach','review','created','wish','praiseup')

         ->from('post')

        ->where('isdeleted','=','0')

        ->and_where_open()

        ->where("description",'like',"%$keyword%")

        ->or_where("tags",'like',"%$keyword%")

        ->and_where_close()

        ->limit($pageSize)

        ->offset($offset)

        ->execute()

        ->as_array();

        return $data;
    }

    public function getSearchItems($keyword,$sort){

         if(!empty($sort)){

            switch ($sort) {
                case 'popularity':

                    $orderby=" order by t.view desc,t.praiseup,t.wish ";

                    break;

                case 'new':

                    $orderby=" order by t.created desc ";

                    break;

                case 'review':

                    $orderby=" order by t.review desc ";

                    break;

                default:

                    $orderby=" order by t.created desc ";
                    break;

            }
        }

        else{

            $orderby=" order by t.created desc ";
        }

        $total= DB::select(array(DB::expr('count(1)'), 'total'))

        ->from('post')

        ->where('isdeleted','=','0')

        ->and_where_open()

        ->where("description",'like',"%$keyword%")

        ->or_where("tags",'like',"%$keyword%")

        ->and_where_close()

        ->execute()

        ->get("total");

        return $total;

    }

    public function getSearchHot(){

        $data=DB::select('keyword')

        ->from('search')

        ->where('isdeleted','=','0')

        ->group_by("keyword")

        ->order_by("number")

        ->limit(10)

        ->execute()

        ->as_array();

        return $data;
    }

    public function saveInfo($keyword){

        $user=Session::instance()->get('user');

        $userid="";

        if(!empty($user)){

            $userid=$user['userid'];

        }
        try{

            Database::instance()->begin();

            $total= DB::select(array(DB::expr('count(1)'), 'total'))

            ->from('search')

            ->where('keyword','=',$keyword)

            ->and_where("userid","=",$userid)

            ->limit(1)

            ->execute()

            ->get("total");

            if($total==0){

                 list($insert_id, $total_rows)= DB::insert('search')

                ->columns(array('userid','keyword','createdon'))

                ->values(array($userid,$keyword,date("Y-m-d h:i:sa")))

                ->execute();

                Database::instance()->commit();

            }

            else{

                DB::update('search')->set(array('number' =>DB::expr("number +1")))

                ->where('userid', '=', $userid)

                ->and_where('keyword','=',$keyword)

                ->execute();

                Database::instance()->commit();

            }

        }
         catch (Exception $e) {

            Database::instance()->rollback();

        }

    }

    //推荐post
    public function recommend(){

        $data=DB::select(array('postid','id'),'title','introduction','description','tags','images','url','price','view','reach','review','created','wish','praiseup','praisedown')
        
        ->from('post')
        
        ->where('isdeleted','=','0')
        
        ->order_by('view','desc')

        ->order_by('praiseup','desc')

        ->order_by('wish','desc')

        ->order_by('postid','desc')
        
        ->limit(20)
        
        ->execute()->as_array();

        return $data;
    }
}